Skip to content

Cache compiled static assets in service worker #2272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hkarani
Copy link
Contributor

@hkarani hkarani commented Jul 8, 2025

Description

A clear and concise description of what you changed and why.
This change patches the cached items array in the service worker at public/sw.js to include Next's compiled static assets emitted during build phase.

WHY?

The service worker ONLY includes the assets in precache-manifest.json injected by Workbox plugin when the Webpack build starts but our static assets only exist later in the build.

Static assets cannot be added in the server build , they are emitted in the client build phase(docs) and are not needed in dev.

However since the precache-manifest.json assets are hardcoded in an array in the generated service worker by Worbox in public/sw.js. We patch it to include our compiled assets after Next compiles and emits the files.

Screenshots

Where workbox stores the items in precache-manifest.json in Q=JSON.parse('the-precache-manifest.json') in the service worker.
Screenshot 2025-07-08 at 19 55 39

Additional Context

Screenshot 2025-07-08 at 19 52 55

Cached items are referenced as /_next/static/chunks/pages/offline-eb65c6c23790b89c.js" depending on the build version.

They static assets urls are formatted to be web urls for them to be accessible by the browser after caching.

Was anything unclear during your work on this PR? Anything we should definitely take a closer look at?

How to test a prod build?

Checklist

Are your changes backward compatible? Please answer below:

For example, a change is not backward compatible if you removed a GraphQL field or dropped a database column.

On a scale of 1-10 how well and how have you QA'd this change and any features it might affect? Please answer below:
7

This change adds the cached static assets in .next/static folder to the service worker in public/sw.js. The changes can be replicated by running:

npm run build

then checking the changes of the public/sw.js in the editor just after Next completes the compilation phase.

To test dev build run:

npm run dev

For frontend changes: Tested on mobile, light and dark mode? Please answer below:
This change will affect the cached offline page.

Did you introduce any new environment variables? If so, call them out explicitly here:
No

cursor[bot]

This comment was marked as outdated.

@hkarani hkarani force-pushed the cache-compiled-static-assets branch from 68b9158 to 1d081f5 Compare July 8, 2025 17:56
cursor[bot]

This comment was marked as outdated.

@hkarani hkarani force-pushed the cache-compiled-static-assets branch from 1d081f5 to 886176f Compare July 8, 2025 17:59
cursor[bot]

This comment was marked as outdated.

@hkarani hkarani force-pushed the cache-compiled-static-assets branch from 886176f to d7bb007 Compare July 8, 2025 18:07
cursor[bot]

This comment was marked as outdated.

@hkarani hkarani requested a review from ekzyis July 9, 2025 08:28
@hkarani hkarani marked this pull request as draft July 9, 2025 08:41
@hkarani hkarani force-pushed the cache-compiled-static-assets branch from d7bb007 to fc85fa9 Compare July 9, 2025 10:08
@hkarani hkarani marked this pull request as ready for review July 9, 2025 10:15
cursor[bot]

This comment was marked as outdated.

@hkarani hkarani force-pushed the cache-compiled-static-assets branch from fc85fa9 to c142b36 Compare July 9, 2025 10:35
cursor[bot]

This comment was marked as outdated.

@hkarani hkarani force-pushed the cache-compiled-static-assets branch from c142b36 to 1c450d0 Compare July 9, 2025 11:01
cursor[bot]

This comment was marked as outdated.

@hkarani hkarani force-pushed the cache-compiled-static-assets branch from 1c450d0 to b7f59e7 Compare July 9, 2025 11:16
cursor[bot]

This comment was marked as outdated.

@hkarani hkarani force-pushed the cache-compiled-static-assets branch from b7f59e7 to c47243a Compare July 9, 2025 11:31
cursor[bot]

This comment was marked as outdated.

@hkarani hkarani force-pushed the cache-compiled-static-assets branch from c47243a to a63f48f Compare July 9, 2025 12:07
cursor[bot]

This comment was marked as outdated.

@hkarani hkarani force-pushed the cache-compiled-static-assets branch from a63f48f to 1caa255 Compare July 9, 2025 13:04
cursor[bot]

This comment was marked as outdated.

@hkarani hkarani force-pushed the cache-compiled-static-assets branch from 1caa255 to 2f9ccd5 Compare July 9, 2025 13:12
@hkarani
Copy link
Contributor Author

hkarani commented Jul 9, 2025

@ekzyis I've implemented a new way to do this mainly because the service worker will only cache items in precache-manifest.json when the build starts, even updating precache-manifest.json mid-webpack build will still cache assets passed when webpack starts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant